#pragma warning(disable:28159)
#pragma warning(disable:4996)
#include <Windows.h>
namespace elibshell {
	inline BOOL sSetPrivilege()
	{
		OSVERSIONINFO osv = { 0 };
		osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
		GetVersionEx(&osv);
		if (osv.dwPlatformId == VER_PLATFORM_WIN32_NT)
		{
			HANDLE hToken;
			TOKEN_PRIVILEGES tkp;

			if (!OpenProcessToken(GetCurrentProcess(), (TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY), &hToken))
				return FALSE;

			LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);

			tkp.PrivilegeCount = 1;
			tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
			AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);

			if (GetLastError() != ERROR_SUCCESS)
				return FALSE;
		}

		return TRUE;
	}
	BOOL MyExitWindows(const INT npMode, const  BOOL blpForceExit)
	{
		if (npMode < 1 || npMode > 5 || sSetPrivilege() == FALSE)
			return FALSE;

		if (npMode >= 4)
			return SetSystemPowerState((npMode == 4), (BOOL)blpForceExit);

		UINT uFlags;
		if (npMode == 1)
		{
			OSVERSIONINFO osv = { 0 };
			osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
			GetVersionEx(&osv);
			if (osv.dwPlatformId == VER_PLATFORM_WIN32_NT)
				uFlags = EWX_POWEROFF;
			else
				uFlags = EWX_SHUTDOWN;
		}
		else
		{
			uFlags = (npMode == 2 ? EWX_REBOOT : EWX_LOGOFF);
		}

		if (blpForceExit)
			uFlags |= EWX_FORCE;

		return ExitWindowsEx(uFlags, 0);
	}

}